package com.bjpowernode.pay.service;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.bjpowernode.common.P2PConstants;
import com.bjpowernode.common.P2PKey;
import com.bjpowernode.model.RechargeRecord;
import com.bjpowernode.pay.config.AlipayConfig;
import com.bjpowernode.service.RechargeService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

@Service
public class AlipayService {

    @Resource
    private AlipayConfig alipayConfig;

    @Resource
    private AlipayClient alipayClient;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @DubboReference(interfaceClass = RechargeService.class)
    private RechargeService rechargeService;
    /**
     * 调用支付宝的下单接口
     * @return 支付宝生成的form表单
     */
    public String  pagePay(Integer uid,String money,String channel) {

        String result = "error";


        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = makeTradeNo();
        //生成充值记录
        RechargeRecord rr = new RechargeRecord();
        rr.setChannel(channel);
        rr.setRechargeDesc("支付宝充值");
        rr.setRechargeMoney(new BigDecimal(money));
        rr.setRechargeNo(out_trade_no);
        rr.setRechargeStatus(P2PConstants.RECHATE_STATUS_PROCESSIGN_0);
        rr.setRechargeTime(new Date());
        rr.setUid(uid);
        int rows = rechargeService.addRecharge(rr);
        if (rows > 0) {
            //设置请求参数
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
            alipayRequest.setReturnUrl(alipayConfig.return_url);
            alipayRequest.setNotifyUrl(alipayConfig.notify_url);

            //付款金额，必填
            String total_amount = money;
            //订单名称，必填
            String subject = "powernode-test";
            //商品描述，可空
            String body = "bjpowernode";

            alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                    + "\"total_amount\":\"" + total_amount + "\","
                    + "\"subject\":\"" + subject + "\","
                    + "\"body\":\"" + body + "\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

            //请求
            try {
                result = alipayClient.pageExecute(alipayRequest).getBody();
                //把生成的订单号，订单的产生时间放进redis-zset里面
                stringRedisTemplate.opsForZSet().add(P2PKey.ALIPAY_PAGE_PAY_NO, out_trade_no, rr.getRechargeTime().getTime());
            } catch (AlipayApiException e) {
                e.printStackTrace();
                return "error";
            }
        }
            return result;
    }




        //生成商家自己的订单号
    private String makeTradeNo(){
        String tradeNo=DateFormatUtils.format(new Date(),"yyyyMMddHHmmssSSS")+
                stringRedisTemplate.opsForValue().increment(P2PKey.ALIPAY_OUTTRADE_NO);
        return tradeNo;
    }
}
